<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="head" v-if="id&&name">
          <el-breadcrumb  separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>{{id?name:''}}</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
          <div class="xian"></div>
        </div>
        <el-form :inline="true" ref="form" :model="form" :rules="rules">
          <el-form-item label="目录名称"><el-input v-model.trim="directoryName" style="width: 192px" size="mini"></el-input></el-form-item>
          <el-form-item class="second" label="状态">
            <el-select v-model="state" placeholder="请选择" style="width: 192px" size="mini">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clear">清除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <i class="el-icon-back" @click="$router.back()" v-if="id&&name">返回学科</i>
            <el-button size="small" type="success" @click="change('add')"><i class="el-icon-edit"></i>新增目录</el-button>
          </el-form-item>
          <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <span>
              <el-form-item label="所属学科" prop="subjectID" v-if="!id && !name">
                <el-select placeholder="请选择" v-model="form.subjectID" style="width: 250px" size="mini">
                  <el-option v-for="item in datlist" :key="item.value" :label="item.label" :value="item.value"></el-option>
                  <el-option label="禁用" value="0"></el-option> </el-select>
              </el-form-item>
              <el-form-item label="目录名称" prop="directoryName">
                <el-input v-model="form.directoryName" placeholder="请输入目录名称" style="width: 250px" size="mini"></el-input>
              </el-form-item>
            </span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </span>
          </el-dialog>
        </el-form>
        <div class="date">
          <span><i class="el-icon-info"></i>数据一共{{ counts }}条</span>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" prop="id" label="序号" width="120px">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="180px">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称" >
          </el-table-column>
          <el-table-column prop="username" label="创建者" >
          </el-table-column>
          <el-table-column label="创建日期">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"></el-table-column>
          <el-table-column prop="state" label="状态" >
            <template slot-scope="scope">
              {{ scope.row.state ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" >
            <template #default="{ row }">
                <el-button type="text" @click="doAllow(row)">{{
              row.state === 1 ? "禁用" : "启用"
            }}</el-button>
              <el-button
                type="text"
                size="small"
                @click="change('edit', row.id)"
                :disabled="row.state === 1"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="del(row.id)"
                :disabled="row.state === 1"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 20px; text-align: right">
          <el-pagination
           :hide-on-single-page="value"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pagesize"
            layout=" prev, pager, next, sizes, jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import {
  list,
  remove,
  changeState,
  add,
  detail,
  update
} from '@/api/hmmm/directorys'
export default {
  data () {
    return {
      id: '',
      name: '',
      state: null,
      directoryName: null,
      page: 1,
      pagesize: 10,
      counts: 0,
      tableData: [],
      dialogType: 'add', // 默认是新增
      dialogVisible: false,
      subjectID: '',
      datlist: '',
      value: true,
      form: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '请选择所属学科' }],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.id = this.$route.query.id
    this.name = this.$route.query.subjectName

    this.getList()
    this.getnewtab()
  },
  methods: {
    async getList () {
      if (this.id) {
        const res = await list({
          page: this.page,
          pagesize: this.pagesize,
          subjectID: +this.id,
          directoryName: this.directoryName,
          state: this.state
        })
        console.log(res)
        this.tableData = res.data.items
        this.counts = res.data.counts
      } else {
        const res = await list({
          directoryName: this.directoryName,
          page: this.page,
          pagesize: this.pagesize,
          state: this.state
        })
        console.log(res)
        this.tableData = res.data.items
        this.counts = res.data.counts
      }
    },
    // 新增确认
    async submit () {
      await this.$refs.form.validate()
      if (this.dialogType === 'add') {
        // console.log(this.form.directoryName, this.id)
        if (this.id) {
          await add({ directoryName: this.form.directoryName, subjectID: +this.id })
        } else {
          await add(this.form)
        }
        this.$message.success('操作成功')
      }
      if (this.dialogType === 'edit') {
        const { subjectID, directoryName, id } = this.form
        await update({ subjectID, directoryName, id })
        this.$message.success('操作成功')
      }
      this.closeDia()
      this.page = 1
      this.getList()
    },
    closeDia () {
      this.dialogVisible = false
      // 冗余数据清空
      this.form = {
        subjectID: '',
        directoryName: ''
      }
      this.$refs.form.resetFields()
    },
    async getnewtab () {
      const res = await simple()
      this.datlist = res.data
      console.log(res)
    },
    // 搜索
    search () {
      this.page = 1
      this.getList()
      // this.state = null
    },
    // 清除
    clear () {
      this.directoryName = null
      this.state = null
      this.getList()
    },
    // 新增,修改同一按钮
    async change (type, id) {
      console.log(type, id)
      this.dialogType = type
      this.dialogVisible = true
      if (type !== 'add') {
        const res = await detail({ id })
        console.log(res)
        this.form = {
          ...res.data
        }
      }
    },
    // 删除
    del (id) {
      console.log(id, '文章')
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除成功')
        this.getList()
      })
    },
    // 切换页码数量
    handleSizeChange (val) {
      console.log(val)
      this.page = 1
      this.pagesize = val
      this.getList()
    },
    // 切换页
    handleCurrentChange (val) {
      console.log(val)
      this.page = val
      this.getList()
    },
    // 点击启用按钮
    async doAllow (row) {
      console.log(row)
      // 取反
      // const state = row.state ? 0 : 1
      // 发送请求更改数据
      // await changeState({ state, id: row.id })
      if (row.state === 1) {
        row.state = 0
        await changeState(row)
        this.$message.success('操作成功')
      } else {
        row.state = 1
        await changeState(row)
        this.$message.success('操作成功')
      }
      // 重新渲染数据
      // this.getList()
    },
    handleClose () {
      this.closeDia()
    }
  },
  computed: {
    dialogTitle () {
      let title = ''
      if (this.dialogType === 'add') title = '新增目录'
      if (this.dialogType === 'edit') title = '修改目录'
      return title
    }
  },
  watch: {
    '$route.query': {
      deep: true,
      handler (val) {
        if (!val.id) {
          this.id = ''
          this.name = ''
          this.getList()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  .head{
margin-bottom: 16px;
.el-breadcrumb{
  margin-bottom: 15px;
}
 .xian {
    width: 100%;
    height: 2px;
    background-color: #fafafa;
  }
  }
 ::v-deep .el-table th {
    background-color: #fafafa;
  }
  .el-input {
    height: 32px;
  }
  .second {
    margin-left: 30px;
  }
  .element.style {
    height: 250px;
  }
  ::v-deep .el-dialog__footer {
    text-align: right;
  }
  .el-icon-back {
    font-size: 14px;
    color: #409eff;
    margin-right: 8px;
  }
  .date {
    margin-bottom: 15px;
    background-color: #f4f4f5;
    padding: 8px 16px;
    border-radius: 4px;
    i {
      color: #909399;
      margin-right: 8px;
    }
    span {
      // padding: 0 8px;
      color: #909399;
      font-size: 14px;
    }
  }
}
</style>
